package com.EE7th.controller;

import com.EE7th.Result.PageResult;
import com.EE7th.Result.Result;
import com.EE7th.domain.Student;
import com.EE7th.dto.PageQueryDTO;
import com.EE7th.service.StudentService;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@CommonsLog
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    //添加学生
    @PostMapping("/add")
    public Result<Void> save(@RequestBody Student student) {
        log.info("save student: " + student);
        studentService.insertStudent(student);
        return Result.success();
    }

    //分页查询
    @GetMapping("/page")
    public Result<PageResult> page(PageQueryDTO pageQueryDTO){
        log.info("page query: " + pageQueryDTO);
        PageResult pageResult = studentService.pageQuery(pageQueryDTO);
        return Result.success(pageResult);
    }

    //根据id查询学生
    @GetMapping("/search/{studentId}")
    public Result<Student> getById(@PathVariable("studentId") int studentId) {
        log.info("根据id查询学生: " + studentId);
        Student student = studentService.getById(studentId);
        return Result.success(student);
    }

    //根据id删除学生
    @DeleteMapping("/delete")
    public Result<Void> delete(@RequestBody List<Integer> ids) {
        log.info("批量删除学生: " + ids);
        studentService.deleteBatch(ids);
        return Result.success();
    }

    //修改学生信息
    @PutMapping("/update")
    public Result<Void> update(@RequestBody Student student) {
        log.info("修改学生信息: " + student);
        studentService.updateStudent(student);
        return Result.success();
    }
}